package com.xp.basic.heima.infofuture21;


import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class JDK21Info {
    public static void main(String[] args) throws InterruptedException {
        //测试字符串模板
//        testStringTemplate();

        Runnable r = ()->{
            System.out.println(STR."\{Thread.currentThread().getName()} HELLO , VT VT VT ");
        };

        //第一种使用虚拟线程的方式，直接使用 ofVirtual
        Thread.ofVirtual().start(r);

        //第二种使用虚拟线程的方式
        Thread.startVirtualThread(r);

        //第三种使用虚拟线程的方式
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        executorService.submit(r);

        //第四种使用虚拟线程的方式
        CustomThread customThread = new CustomThread();
        ThreadFactory factory = Thread.ofVirtual().factory();
        Thread thread = factory.newThread(customThread);
        thread.start();


        TimeUnit.SECONDS.sleep(3);


    }

   static class CustomThread implements Runnable{

        /**
         * Runs this operation.
         */
        @Override
        public void run() {
            System.out.println("custom thread -----");
        }
    }

    /**
     * 测试字符串模板
     */
    private static void testStringTemplate() {
        //        STR."Getting \{name}!";
        String name = "123";
        String s = StringTemplate.STR."dadads\{name}";
        System.out.println("s = " + s);

        int x = 10,y = 20;

        String s1 = STR."\{x} + \{y} = \{x + y}";
        System.out.println("s1 = " + s1);

        String s2 = STR."\{x} * \{y} = \{x * y}";
        System.out.println("s2 = " + s2);

        String time = STR."The current time is \{
                //sample comment - current time in HH:mm:ss
                DateTimeFormatter
                        .ofPattern("HH:mm:ss")
                        .format(LocalTime.now())
                }.";
        System.out.println(STR."time = \{time}");

        String  a = "a";
        String  b = "b";

        String s3 = STR."\{a} + \{b} = \{a + b}";
        System.out.println(STR."s3 = \{s3}");
    }
}
